package code.bigmama.activity;

import code.bigmama.R;
import code.bigmama.context.LoginContext;
import code.bigmama.controller.DBController;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class LoginActivity extends Activity
{
	public static final String	DEBUG_TAG			= "LoginActivity";

	EditText					mID					= null;
	EditText					mPassword			= null;
	CheckBox					mSaveID				= null;
	CheckBox					mAutoLogin			= null;
	Button						mLoginButton		= null;
	Button						mSigninButton		= null;
	DBController				mDatabaseController	= null;
	
	private static final int REQUEST_CODE_FOR_MAIN_ACTIVITY = 1;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		Log.d(DEBUG_TAG, "OnCreate()");
		setContentView(R.layout.login);
		setResId();
		setListener();

		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode == Activity.RESULT_OK)
		{
			if (requestCode == REQUEST_CODE_FOR_MAIN_ACTIVITY)
			{
				data.getStringExtra("REAULT_FOR_LOGIN");
				finish();
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	private void setResId()
	{
		mID = (EditText) findViewById(R.id.login_id);
		mPassword = (EditText) findViewById(R.id.login_passwd);
		mSaveID = (CheckBox) findViewById(R.id.login_save_id);
		mAutoLogin = (CheckBox) findViewById(R.id.login_auto_login);
		mLoginButton = (Button) findViewById(R.id.login_login_button);
		mSigninButton = (Button) findViewById(R.id.login_signin_button);
	}

	private void setListener()
	{
		mID.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{

			}
		});

		mPassword.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
			}
		});

		mLoginButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				checkIdAndPassword();
			}
		});

		mSigninButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent i = new Intent();
				i.setClass(getBaseContext(), code.bigmama.activity.SigninActivity.class);
				startActivity(i);
			}
		});
	}

	private void checkIdAndPassword()
	{
		mDatabaseController = DBController.getInstance();
		mDatabaseController.open();

		LoginContext context = mDatabaseController.getIdAndPasswd();

		if (context == null)
		{
			Log.e(DEBUG_TAG, "LoginContext is null");
			return;
		}

		if (context.getId().equals(mID.getText().toString()) == true)
		{
			if (context.getPassword().equals(mPassword.getText().toString()) == true)
			{
				Intent i = new Intent();
				i.setClass(getBaseContext(), code.bigmama.activity.MainActivity.class);
				startActivityForResult(i, REQUEST_CODE_FOR_MAIN_ACTIVITY);
			}
		}

	}

	@Override
	protected void onResume()
	{
		Log.d(DEBUG_TAG, "onResume()");
		super.onResume();
	}

	@Override
	protected void onStart()
	{
		Log.d(DEBUG_TAG, "onStart()");
		super.onStart();
	}

	@Override
	protected void onRestart()
	{
		Log.d(DEBUG_TAG, "onRestart()");
		super.onRestart();
	}

	@Override
	protected void onStop()
	{
		Log.d(DEBUG_TAG, "onStop()");
		super.onStop();
	}

	@Override
	protected void onDestroy()
	{
		Log.d(DEBUG_TAG, "onDestroy()");

		if (mDatabaseController != null)
		{
			mDatabaseController.close();
			mDatabaseController = null;
		}

		super.onDestroy();
	}

}
